//index.js
import http from '../../../../class/Utils/Http.js'
import user from '../../../../class/Utils/User.js'
import tips from '../../../../class/Utils/Tips.js'
var WxParse = require('../../../../class/wxParse/wxParse.js');
var loadMore = function (that) {
    // wx.showLoading({ 'title': "加载中" })
    //tips.loading("为啥不显示")
    var name = that.data.name
    var url = '/syq/lists'
    http.req(url, {name: name, page: 1}, function (res) {


        if (res.pw_rec_list) {
            var prolist = that.data.oplist;
            for (var i = 0; i < res.pw_rec_list.length; i++) {
                if (res.pw_rec_list[i].type == 2) {
                    res.pw_rec_list[i].typecls = 'gongying';

                }
                if (res.pw_rec_list[i].type == 1) {
                    res.pw_rec_list[i].typecls = 'qiugou';

                }
                if (res.pw_rec_list[i].type == 3) {
                    res.pw_rec_list[i].typecls = 'zhaoshang';

                }
                if (res.pw_rec_list[i].type == 4) {
                    res.pw_rec_list[i].typecls = 'shangfu';

                }
                res.pw_rec_list[i].changduans = 0;
                res.pw_rec_list[i].liuyans = 0;
                res.pw_rec_list[i].shoucangs = 0;
                prolist.push(res.pw_rec_list[i]);
            }
            that.setData({
                lock: 1,
                oplist: prolist,
                qid: res.q_id
            });
            that.setData({
                page: 2
            })


            console.log(that.data.oplist)

        } else {
            wx.showToast({
                title: '暂无数据',
                icon: 'success',
                duration: 2000
            })
        }
    });


    http.req('/syq/detail', {name: name}, function (res) {
        if (res) {

            that.setData({
                intro: res
            })
            WxParse.wxParse('content', 'html', res.intro, that, 5);

            //遍历好友查看圈主是否为好友

            var user = wx.getStorageSync('user')
            if (user) {
                var data = {
                    uid: user.userid
                }
                http.req("/Api/haoyou_list", data, function (res) {

                  

                    that.setData({
                        quanhaoyou: res.list
                    })

                    if (res.list['#']) {

                        for (var i = 0; i < res.list['#'].length; i++) {
                            if (res.list['#'][i].friend_login == that.data.intro.master) {
                                console.log(res.list['#'][i].friend_login)
                                that.setData({
                                    qzhy: 1
                                })
                                break
                            }
                        }
                    }
                    for (var i = 65; i < 91; i++) {

                        var a = String.fromCharCode(i)
                        if (res.list[a]) {
                            for (var j = 0; j < res.list[a].length; j++) {
                                if (res.list[a][j].friend_login == that.data.intro.master) {
                                    that.setData({
                                        qzhy: 1
                                    })
                                    break
                                }
                            }
                        }
                    }
                })
            }
        } else {
            wx.showToast({
                title: '暂无数据',
                icon: 'success',
                duration: 2000
            })

        }


    });
    //判断是否已加圈
    var user = wx.getStorageSync('user')
    if (user) {
        http.req("/Api/quan_list", {uid: user.userid}, function (res) {
            if (res.count > 0) {
                for (var j = 0; j < res.count; j++) {
                    if (res.list[j].url == that.data.name) {
                        that.setData({
                            yjjq: 1
                        })
                        break
                    }
                }
            }
        })
    }

    var url = '/syq/members'

    http.req(url, {name: name, page: 1}, function (res) {

        if (res.list) {

            console.log('sshhhhhhhhhhhhhhh')
            var quanhaoyou = that.data.quanhaoyou
            var alphabit = that.data.alphabit
            for (var i = 0; i < alphabit.length; i++) {


                if (res.list[alphabit[i]]) {

                    for (var j = 0; j < res.list[alphabit[i]].length; j++) {


                        for (var k = 0; k < alphabit.length; k++) {

                            if (quanhaoyou[alphabit[k]]) {

                                for (var l = 0; l < quanhaoyou[alphabit[k]].length; l++) {

                                    if (res.list[alphabit[i]][j].poster == quanhaoyou[alphabit[k]][l].friend_login) {
                                        res.list[alphabit[i]][j].isfrnd = 1
                                    }

                                }

                            }
                        }

                    }

                }
            }


            that.setData({
                member: res.list
            })


        } else {
            wx.showToast({
                title: '无记录',
                icon: 'success',
                duration: 2000
            })

        }
        wx.hideLoading()
    });
    //tips.loaded()

}


var loadMore1 = function (that) {

    wx.showLoading({
        title: '加载中',
    })
    var page = that.data.page
    var name = that.data.name
    var url = '/syq/lists'
    http.req(url, {name: name, page: page}, function (res) {

        if (res.pw_rec_list) {

            var prolist = that.data.oplist;
            for (var i = 0; i < res.pw_rec_list.length; i++) {
                if (res.pw_rec_list[i].type == 2) {
                    res.pw_rec_list[i].typecls = 'gongying';

                }
                if (res.pw_rec_list[i].type == 1) {
                    res.pw_rec_list[i].typecls = 'qiugou';

                }
                if (res.pw_rec_list[i].type == 3) {
                    res.pw_rec_list[i].typecls = 'zhaoshang';

                }
                if (res.pw_rec_list[i].type == 4) {
                    res.pw_rec_list[i].typecls = 'shangfu';

                }
                res.pw_rec_list[i].changduans = 0;
                res.pw_rec_list[i].liuyans = 0;
                res.pw_rec_list[i].shoucangs = 0;
                prolist.push(res.pw_rec_list[i]);
            }
            that.setData({
                lock: 1,
                oplist: prolist
            });
            page++
            that.setData({
                page: page
            })


        } else {
            wx.showToast({
                title: '暂无数据',
                icon: 'success',
                duration: 2000
            })


        }
        wx.hideLoading()
    });

}


Page({
    data: {
        quanmenuid: '1',
        quanmenuname: '圈商机',
        liuyantag: false,
        quanmenuopen: false,
        headheight: '',
        name: "",
        page: 1,
        oplist: [],
        member: [],
        page2: 1,
        qid: 0,
        pageonescroll: '',
        scrollNow: 0,
        liuyan_list_state: false,
        qzhy: 0,
        yjjq: 0,
        nolist: 0,
        xiaoxi_count: 0
    },
    onLoad: function (options) {

        var that = this

        var alphabit = []
        for (var i = 65; i < 91; i++) {
            alphabit.push(String.fromCharCode(i))
        }
        alphabit.push("#")
        that.setData({
            alphabit: alphabit
        })


        that.Huoquheadheight()

        var name = options.name,
            title = options.title
        console.log(title)

        that.setData({
            name: name,
            quanname: title
        })

        wx.setNavigationBarTitle({
            title: title
        })

        loadMore(that);


    },

    /*
     消息
     */
    xiaoxi: function () {
        var that = this
        /*
         log_user 返回  缓存user
         */
        user.log_user(this, function (res) {
            tips.goto("/pages/mingpian/talk/list/list");
        })
    },
    onShow: function () {
        var that = this
        var user = wx.getStorageSync('user')
        if (user) {
            if (user.log == 1) {
                /*
                 获取未读消息数量
                 */
                http.req('/Mingpian/xiaoxi', {uid: user.userid}, function (res) {
                    console.log(res)
                    that.setData({
                        xiaoxi_count: res
                    })
                })
            }
        }
    },


    onReachBottom: function () {
        var that = this
        var thismenuid = that.data.quanmenuid
        if (thismenuid == 1) {
            loadMore1(that);
        }

    },


    //获取顶部高度
    Huoquheadheight: function () {
        var that = this
        wx.createSelectorQuery().selectAll('.el-shangji-head').boundingClientRect(function (rects) {
            rects.forEach(function (rect) {
                that.setData({
                    headheight: rect.height
                })
            })
        }).exec()
    },


    //搜索跳转页面
    searchlink: function (e) {
        var that = this
        wx.navigateTo({
            url: '/pages/weishangji/search/search'
        })
    },

    //选择圈导航
    quanmenuselect: function (e) {
        var that = this
        var thisid = e.currentTarget.dataset.menuid
        var thisname = e.currentTarget.dataset.menuname
        that.setData({
            quanmenuid: thisid,
            quanmenuname: thisname
        })
        that.setData({
            quanmenuopen: false
        })
        var pageonescroll = that.data.pageonescroll
        if (thisid == 1) {
            console.log(pageonescroll)
            wx.pageScrollTo({
                scrollTop: pageonescroll,
                duration: 0,
            })
        } else {
            console.log(pageonescroll)
            wx.pageScrollTo({
                scrollTop: 0,
                duration: 0,
            })
        }

    },
    //打开留言
    liuyanbtn: function (e) {
        var that = this
        var mid = e.currentTarget.dataset.id
        var index = e.currentTarget.dataset.index
        var list = that.data.oplist
        user.log_user(that, function () {
            list[index].liuyan_state = !list[index].liuyan_state
            that.setData({
                oplist: list
            })
            /*获取留言内容*/
            if (list[index].liuyan_state) {
                var data = {
                    uid: wx.getStorageSync('user').userid,
                    mid: mid
                }
                http.reqload("/Api/liuyan_list", data, function (res) {
                    // console.log(res)
                    list[index].liuyan_list = res
                    if (list[index].liuyan_list.length > 5) {
                        list[index].liuyan_list.splice(5, 1)
                        list[index].liuyan_gengduo = 1
                    }
                    that.setData({
                        oplist: list
                    })
                })
            }

        })

    },
    /*
     输入内容
     */
    content: function (e) {
        var that = this
        var index = e.currentTarget.dataset.index
        var list = that.data.oplist
        list[index].liuyan_content = e.detail.value
        this.setData({
            oplist: list
        })
    },
    liuyan: function (e) {
        var that = this
        var user = wx.getStorageSync("user")
        var mid = e.currentTarget.dataset.id
        var index = e.currentTarget.dataset.index
        var list = that.data.oplist
        if (list[index].liuyan_content == '') {
            tips.error('请输入留言内容')
            return
        }
        list[index].liuyan_disabled = true
        that.setData({
            oplist: list
        })
        var data = {
            uid: user.userid,
            content: list[index].liuyan_content,
            mid: mid
        }


        http.reqload("/Api/add_liuyan", data, function (res) {
            if (res.code == '202') {
                console.log(res);
                tips.ok(res.msg_val)
                list[index].liuyan_list.unshift({'poster': res.poster, 'content': res.content})
                list[index].liuyan_disabled = false
                list[index].liuyan_content = ''
                if (list[index].liuyan_list.length > 5) {
                    list[index].liuyan_list.splice(5, 1)
                    list[index].liuyan_gengduo = 1
                }
                that.setData({
                    oplist: list
                })
            } else {
                tips.error(res.msg_val)
            }
        })
        console.log(list)
    },


    //收藏/取消收藏
    shoucangbtn: function (e) {


        var that = this
        /*
         log_user 返回  缓存user
         */
        user.log_user(this, function (res) {

            var id = e.currentTarget.dataset.id
            var presentindex = e.currentTarget.dataset.index
            var datalist = that.data.oplist
            var datalistzhi = datalist[presentindex].shoucangs

            if (datalistzhi == 0) {

                var data = {
                    uid: res.userid,
                    tid: id
                }

                http.req("/Api/add_shoucang", data, function (res) {
                    if (res.code == '203') {
                        tips.ok(res.msg_val);


                        datalist[presentindex].shoucangs = 1

                        that.setData({
                            oplist: datalist
                        })

                    } else {
                        tips.error(res.msg_val)
                    }
                })

            } else {


            }


        })


    },
    //打开隐藏圈栏目导航
    quanmenuopenbtn: function (e) {
        var that = this
        var quanmenuopentag = that.data.quanmenuopen
        if (quanmenuopentag == false) {
            that.setData({
                quanmenuopen: true
            })
        } else {
            that.setData({
                quanmenuopen: false
            })
        }
    },
    //去首页
    gotohome: function () {
        wx.switchTab({
            url: '/pages/index/index'
        })
    },
    //去全部圈
    gotoshengyiquan: function () {
        wx.switchTab({
            url: '/pages/shengyiquan/list/list'
        })
    },
    //去会员中心
    gotomaincenter: function () {
        wx.switchTab({
            url: '/pages/main/menu/menu'
        })
    },
    //圈成员字母查找
    zimuscroll: function (e) {
        var that = this
        var thiszimu = e.currentTarget.dataset.zimu

        console.log(thiszimu)

        if (thiszimu == '#')
            thiszimu = ''


        //获取当前选择字母的节点 begin
        wx.createSelectorQuery().selectAll('.zimu' + thiszimu).boundingClientRect(function (rects) {

            rects.forEach(function (rect) {
                var zimujuli = rect.top

                var dataheadheight = that.data.headheight

                console.log(dataheadheight)

                var scrollweizhi = zimujuli - dataheadheight
                wx.pageScrollTo({
                    scrollTop: scrollweizhi + that.data.scrollNow,
                })
            })
        }).exec()
        //获取当前选择字母的节点 end


    },
    //监听 page 滚动条位置
    onPageScroll: function (res) {
        var that = this;
        var topscroll = res.scrollTop;
        var quanmenuid = that.data.quanmenuid

        if (quanmenuid == 1) {
            that.setData({
                pageonescroll: topscroll
            })
            console.log('圈商机当前滚动条位置' + that.data.pageonescroll)
        }
        if (quanmenuid == 3) {
            that.setData({
                scrollNow: topscroll
            })
        }
    },

    //长短 介绍切换
    changeintro: function (e) {
        var that = this
        console.log(e)
        var presentindex = e.currentTarget.dataset.index
        var datalist = that.data.oplist
        var datalistzhi = datalist[presentindex].changduans

        for (var i = 0; i < datalist.length; i++) {

            if (datalistzhi == 1) {
                datalist[presentindex].changduans = 0

            }
            else {
                datalist[presentindex].changduans = 1
            }
        }


        that.setData({
            oplist: datalist
        })
        console.log(datalist)


    },
    /*
     添加圈主
     */
    add_qz: function (e) {
        var that = this
        /*
         log_user 返回  缓存user
         */
        user.log_user(this, function (res) {
            var id = e.currentTarget.dataset.id
            var poster = e.currentTarget.dataset.name
            var data = {
                uid: res.userid,
                id: id,
                poster: poster
            }
            console.log(data)
            http.req("/Api/add_haoyou", data, function (res) {
                console.log(res)
                if (res.code == '206') {
                    tips.ok(res.msg_val)
                    that.setData({
                        qzhy: 1
                    })
                } else {
                    tips.error(res.msg_val)
                }
            })
        })
    },
    /*
     添加好友
     */
    add_haoyou: function (e) {
        var that = this
        /*
         log_user 返回  缓存user
         */
        user.log_user(this, function (res) {
            var index = e.currentTarget.dataset.index
            var order = e.currentTarget.dataset.order
            var id = e.currentTarget.dataset.id
            var poster = e.currentTarget.dataset.name
            var list = that.data.member


            var data = {
                uid: res.userid,
                id: id,
                poster: poster
            }


            http.req("/Api/add_haoyou", data, function (res) {
                console.log(res)
                if (res.code == '206') {
                    tips.ok(res.msg_val)

                    list[order][index].isfrnd = 1
                    that.setData({
                        member: list
                    })

                } else {
                    tips.error(res.msg_val)
                }
            })
        })
    },
    /*
     授权登录
     */
    bindGetUserInfo: function (e) {
        var that = this
        user.user(this, function (res) {
            that.setData({
                shouquan: 0,
                userinfo: res

            })
            if (res.log == 0) {
                that.setData({
                    bangdingtoocletag: true
                })
            }
        })

    },
    /*
     弹出 登录
     */
    bangdingtoocle: function () {
        var that = this
        that.setData({
            bangdingtoocletag: !that.data.bangdingtoocletag
        })
    },
    /*
     点击登录
     */
    denglu: function (e) {
        var that = this
        var zhanghao = e.detail.value.zhanghao
        var mima = e.detail.value.mima
        if (!zhanghao) {
            tips.error("账号不能为空");
            return false
        }
        if (!mima) {
            tips.error("密码不能为空");
        }
        http.req("/Api/log", {login: zhanghao, passwd: mima, uid: wx.getStorageSync('user').userid}, function (res) {
            console.log(res)
            if (res.code == '200') {
                var user = wx.getStorageSync('user')
                user.log = 1
                wx.setStorageSync('user', user)
                tips.ok('登录成功')
                that.setData({
                    bangdingtoocletag: false,
                    shengyihao: zhanghao
                })
            } else {
                tips.error(res.msg_val)
            }
        })
    },
    /*
     发布商机链接
     */
    fabusj: function () {
        var that = this
        /*
         log_user 返回  缓存user
         */
        user.log_user(this, function (res) {
            tips.goto("/pages/weishangji/add/add");
        })
    },
    /*
     加入生意圈
     */
    jiaru: function (e) {
        var that = this
        user.log_user(that, function (ures) {
            var id = e.currentTarget.dataset.id
            var name = e.currentTarget.dataset.name
            tips.modal("是否加入 '" + name + "'", true, 'function', false, function () {

                var data = {
                    uid: ures.userid,
                    qid: id
                }
                console.log(data)
                http.req("/Api/add_quan", data, function (res) {
                    console.log(res)
                    if (res.code == '204') {
                        tips.ok(res.msg_val)

                        that.setData({
                            yjjq: 1
                        })

                    } else {
                        tips.error(res.msg_val)
                    }
                })
            })

        })
    },

    /*
     点击名片
     */
    mingpian: function (e) {
        var that = this
        user.log_user(this, function (res) {
            var poster = e.currentTarget.dataset.poster
            var cid = e.currentTarget.dataset.cid
            if (cid) {
                var dd = {
                    poster: poster,
                    cid: cid
                }
                http.reqload("/Api/mingpian", dd, function (res) {
                    console.log(res)
                    if (res.code == 200) {
                        tips.goto('/pages/mingpian/card/card?id=' + res.mid)
                    } else {
                        tips.error(res.msg_val)
                    }
                })
            } else {
                tips.error("暂无名片")
            }

        })
    },
    //点击图片放大
    previewImage: function (e) {
        var that = this;
        console.log(e)


        //获取点击的数据里的图片

        var thisindex = e.currentTarget.dataset.index
        var thatdatalist = that.data.oplist
        var thatdatalistindex = thatdatalist[thisindex].pic_list_img

        var newimgdata = [];
        for (var i = 0; i < thatdatalistindex.length; i++) {
            if (thatdatalistindex[i]) {
                newimgdata.push('http://img.i.album.toocle.com/0-0-0/' + thatdatalistindex[i]);
            }

        }
        that.setData({
            newimgdata: newimgdata,
        });

        console.log(newimgdata)


        var current = e.currentTarget.dataset.src;
        wx.previewImage({
            current: current, // 当前显示图片的http链接
            urls: that.data.newimgdata // 需要预览的图片http链接列表
        })
    },
  //获取绑定微信的手机号码
  getPhoneNumber: function (e) {
    var that = this
    user.getPhoneNumber(e, that)
  },

})
